<?php

namespace Portal\RequestTutorBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;


use Portal\RequestTutorBundle\Entity\RequestTutor;
use Portal\RequestTutorBundle\Form\RequestTutorType;



class RequestTutorController extends Controller
{
    public function newAction()
    {

      $entity = new RequestTutor();
      $form = $this->createForm(new RequestTutorType(), $entity);

      return $this->render('PortalRequestTutorBundle:RequestTutor:new.html.twig', array(
                  'entity' => $entity,
                  'form' => $form->createView(),
              ));
    }
    
    public function createAction(Request $request)
    {
      $entity = new RequestTutor();
      $form = $this->createForm(new RequestTutorType(), $entity);
      $form->bind($request);
      if ($form->isValid()) {
        
        $em = $this->getDoctrine()->getManager();
        $entity->setStatus($this->container->getParameter('requestCreated'));
        $entity->setViewCount($this->container->getParameter('viewCountZero'));        
        $entity->setUpdatedAt(new \DateTime('NOW'));
        $entity->setCreatedIp($_SERVER['REMOTE_ADDR']);        
        $em->persist($entity);
        $em->flush();

        $this->get('session')->setFlash('notice', 'Хүсэлт амжилттай бүртгэгдлээ.');
        return $this->redirect($this->generateUrl('homepage'));
      }

      $this->get('session')->setFlash('error', 'Хүсэлт амжилттай бүртгэгдлээ.');
      return $this->render('PortalRequestTutorBundle:RequestTutor:new.html.twig', array(
                  'entity' => $entity,
                  'form' => $form->createView(),
              ));
    }
}
